package sgg_hsp_data_structures._03_linkedlist;

public class SingleLinkedListDemo {


    public static void main(String[] args) {
        SingleLinkedList singleLinkedList = new SingleLinkedList();

        HeroNode heroNode1 = new HeroNode(1, "A", "AA");
        HeroNode heroNode2 = new HeroNode(2, "B", "BB");
        HeroNode heroNode3 = new HeroNode(3, "C", "CC");
        HeroNode heroNode4 = new HeroNode(4, "D", "DD");

//        singleLinkedList.add(heroNode1);
//        singleLinkedList.add(heroNode3);
//        singleLinkedList.add(heroNode4);
//        singleLinkedList.add(heroNode2);

        singleLinkedList.addByOrder(heroNode1);
        singleLinkedList.addByOrder(heroNode3);
        singleLinkedList.addByOrder(heroNode4);
        singleLinkedList.addByOrder(heroNode2);

        singleLinkedList.list();

        System.out.println("-----------");
        HeroNode newHeroNode = new HeroNode(2, "Lu", "HXDM");
//        singleLinkedList.update(newHeroNode);
//        singleLinkedList.list();

        System.out.println("----del-------");
//        singleLinkedList.del(2);
//        singleLinkedList.list();

        System.out.println("----reverse-------");
        singleLinkedList.reverseList11();
        singleLinkedList.list();

    }
}

class SingleLinkedList {

    private HeroNode head = new HeroNode(0, "", "");

    public void list() {
        if (head.next == null) {
            System.out.println("Empty  list");
            return;
        }

        HeroNode temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }

    public void add(HeroNode heroNode) {
        HeroNode temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = heroNode;
    }

    public void addByOrder(HeroNode heroNode) {
        HeroNode temp = head;

        boolean isIDExist = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no > heroNode.no) {  // 位置找到就在temp后面插入
                break;
            } else if (temp.next.no == heroNode.no) {
                isIDExist = true;
                break;
            }
            temp = temp.next;
        }

        if (isIDExist) {
            System.out.println("Already exist");
        } else {
            heroNode.next = temp.next;
            temp.next = heroNode;
        }
    }

    public void update(HeroNode newHeroNode) {
        if (head.next == null) {
            System.out.println("Empty  list");
            return;
        }

        HeroNode temp = head.next;
        boolean isFound = false;
        while (true) {
            if (temp == null) {
                break;
            }

            if (temp.no == newHeroNode.no) {// Found
                isFound = true;
                break;
            }
            temp = temp.next;
        }

        if (isFound) {
            temp.name = newHeroNode.name;
            temp.nickName = newHeroNode.nickName;
        } else {
            System.out.println("Not Found");
        }

    }

    public void del(int no) {
        HeroNode temp = head;
        boolean isFound = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no == no) {
                isFound = true;
                break;
            }
            temp = temp.next;
        }
        if (isFound) {
            temp.next = temp.next.next;
        } else {
            System.out.println("Not  Found  to be delete!");
        }
    }

    /**
     * TX面试题反转单链表
     */
    public void reverseList11() {

        if (head.next == null || head.next.next == null) {
            return;
        }

        HeroNode cur = head.next;
        HeroNode next = null;
        HeroNode reverseHead = new HeroNode(0, "", "");
        // 头删 头插
        while (cur != null) {
            // 保存下一个节点
            next = cur.next;
            // 相当于已经取出了；当前节点的下一个 指向新头节点的下一个元素
            cur.next = reverseHead.next;

            // 新节点安插到新head的最前面（头插）
            reverseHead.next = cur;
            //移动到下一个待处理元素
            cur = next;
        }
        head.next = reverseHead.next;

    }


}

class HeroNode {
    public int no;
    public String name;
    public String nickName;
    public HeroNode next;

    public HeroNode(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}





